package com.yh.spring.struts.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yh.spring.mybatis.model.Order;
import com.yh.spring.mybatis.model.Page;
import com.yh.spring.mybatis.service.IOrderService;

public class OrderAction extends ActionSupport implements ModelDriven<Object> {
	private static final long serialVersionUID = 1L;

	@Autowired
	private IOrderService orderService;

	private Order order;
	private int pageSize;
	private int pageNum;
	
	private Page page;

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
	public String findOrder(){
		order=orderService.getOrder(order);
		SimpleDateFormat timeFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = timeFormater.format(order.getOrder_date());
		
		ServletActionContext.getRequest().setAttribute("data", order);
		ServletActionContext.getRequest().setAttribute("date", str);
		//System.out.println(order.getOrder_date());
		return "success";
		
	}
	public String addOrder(){
		orderService.addOrder(order);
		return "success";
	}
	
	public String delOrder(){
		orderService.delOrder(order);
		return "success";
	}
	
	public String editOrder(){
		orderService.editOrder(order);
		return "success";
	}

	public String queryOrder(){
		Map<String,Object> cond=new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("order", order);
		page=orderService.queryOrder(cond);
		return "success";		
	}
	
	@Override
	public Object getModel() {
		if(order==null){
			order = new Order();
		}
		return order;
	}
}
